package com.example.demo.netty.chat_room.client;

import com.example.demo.netty.chat_room.domain.MessageProtocol;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class ClientHandler extends SimpleChannelInboundHandler<MessageProtocol> {
    /**
     * 做一下缓冲区容量测试,看看能一次读多少数据
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
//        StringBuilder stringBuilder=new StringBuilder();
//        for (int i = 0; i <11000000 ; i++) {
//            stringBuilder.append("测试长度测试长度测试长度测试长度测试长度测试长度");
//        }
//        byte[] bytes = stringBuilder.toString().getBytes();
//        ctx.write(new MessageProtocol(bytes.length,bytes));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println(cause.getMessage());
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageProtocol messageProtocol) throws Exception {
        System.out.println(new String(messageProtocol.getData())+"@@"+messageProtocol.getLength());
    }
}
